// pages/goodsManagement/setVip/setVip.js
const Controllers = require('../../controllers/Controller.js').controller;
const formatTime3 = require('../../../utils/util.js').formatTime3
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:false,
    isGiveBalance:false,
    isGiveIntegral:false,
    isGiveCoupon:false,
    couponList:[],
    addCouponList:[],
    vipPrice:'',
    balance:'',
    integral:''

  },
  //隐藏弹窗
  hideAddCoupon(){
    this.setData({
      isShow:false
    })
  },
  //显示弹窗
  addCoupon(){
    if(this.data.couponList.length>0)
    this.setData({
      isShow:true
    })
    else
    wx.showToast({
      title: '暂无优惠券可选',
    })
  },
  //提交
  submit(){
    let data
    let that =this
    let ids = ""
    this.data.addCouponList.forEach((item,index)=>{
      
      if(index==0)
      ids=ids+item.id+"-1"
      else
      ids=ids+","+item.id+"-1"
      console.log(ids)
    })
   // ids = JSON.stringify(ids)
    data = {
      price:this.data.vipPrice,
      type:2,
      haveGivePrice: this.data.isGiveBalance ? 1 : 0,
      shopId:wx.getStorageSync('shopId'),
      haveGiveIntegral: this.data.isGiveIntegral ? 1:0,
      giveIntegral:this.data.isGiveIntegral ? this.data.integral :'',
      givePrice:this.data.isGiveBalance?this.data.balance : '',
      goodsIdNum:this.data.isGiveCoupon ? ids : '',
      haveGoods:this.data.isGiveCoupon?1:0,
      name:"会员开卡",
      timeState:1
    }
    if(this.data.vipPrice == ''){
      wx.showToast({
        title: '请输入开卡金额',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.isGiveIntegral && this.data.integral == ''){
      wx.showToast({
        title: '请输入赠送积分',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.isGiveBalance && this.data.balance == ''){
      wx.showToast({
        title: '请输入赠送余额',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.isGiveCoupon && ids == '[]'){
      wx.showToast({
        title: '请选择优惠券',
        icon: 'none',
        duration: 2000
      })
    }else{
      if(that.data.id){
        data.id=that.data.id,
        
        data.sessionId=wx.getStorageSync('token'),
        Controllers.updateOpenVip(data).then(res=>{
          if(res.code==1){
            wx.navigateBack({
              delta: 1
            })
            wx.showToast({
              title: '会员开卡设置成功',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }else{
        Controllers.save(data).then(res=>{
          if(res.code==1){
            wx.navigateBack({
              delta: 1
            })
            wx.showToast({
              title: '会员开卡设置成功',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }


    
    }
    
  },
  //开关
  switchGiveBalance(e){
    this.setData({
      isGiveBalance:e.detail.value
    })
  },
  switchGiveIntegral(e){
    this.setData({
      isGiveIntegral:e.detail.value
    })
  },
  switchGiveCoupon(e){
    this.setData({
      isGiveCoupon:e.detail.value
    })
  },
  //输入框赋值
  bindinput(e){
    let type = e.currentTarget.dataset['type'];
    let value = e.detail.value
    if(type=="vipPrice"){
      this.setData({
        vipPrice:value
      })
    }else if(type=="balance"){
      this.setData({
        balance:value
      })
    }else if(type=="integral"){
      this.setData({
        integral:value
      })
    }
  },
  //确认选择
  determine(){
    let addCouponList
    addCouponList = this.data.couponList.filter(item=>item.select)
    this.setData({
      addCouponList,
      isShow:false
    })
  },
  //选择
  select(e){
    let index = e.currentTarget.dataset['index'];
    let couponList = this.data.couponList
    couponList[index].select = !couponList[index].select
    this.setData({
      couponList
    })
  },
  //删除
  delect(e){
    let index = e.currentTarget.dataset['index'];
    let addCouponList = this.data.addCouponList
    addCouponList.splice(index,1)
    this.setData({
      addCouponList
    })
  },
  //获取优惠券列表
  activityPage(){
    let that =this
    Controllers.shopActivityPage(
      {shopId:wx.getStorageSync('shopId'),sessionId:wx.getStorageSync('token'),state:0,pageNumber:1,pageSize:20}).then(res=>{
      let couponList = []
      if(res.code==1){
        res.data.list.forEach(item => {
          console.log(item)
          if(item.type!=0){
            // if(item.effectiveStartTime)
            // item.effectiveStartTime = formatTime3(item.effectiveStartTime)
            // if(item.effectiveEndTime)
            // item.effectiveEndTime = formatTime3(item.effectiveEndTime)
            if(item.type == 1){
              item.typeText="满减优惠券"
            }else if(item.type == 2){
              item.typeText="限时限产品券"
            }else if(item.type == 3){
              item.typeText="免费体验券"
            }else if(item.type == 4){
              item.typeText="活动礼包券"
            }else if(item.type == 5){
              item.typeText="拉新券"
            }
            couponList.push(item)
          }
        });
        this.setData({
          couponList
        })

        that.getVipSetting()
      }
    })
  },

  getVipSetting(){
    let that =this
    Controllers.getMyOpenVip(
      {shopId:wx.getStorageSync('shopId'),
      sessionId:wx.getStorageSync('token')}).then(res=>{
      if(res.code==1){
        
        let couponList= that.getArrEqual(res.data.vipList,that.data.couponList)
        that.setData({
          isGiveBalance:true,
          balance:res.data.givePrice,
          vipPrice:res.data.price,
          integral:res.data.giveIntegral,
          isGiveIntegral:res.data.giveIntegral>0,
          isGiveCoupon:res.data.vipList.length>0,
          id:res.data.id,
          addCouponList:res.data.vipList,
          couponList:couponList
        })
      }
    })
  },

    getArrEqual(arr1, arr2) {
        let newArr = [];
        for (let i = 0; i < arr2.length; i++) {
            for (let j = 0; j < arr1.length; j++) {
                if(arr1[j].id === arr2[i].id){
                  arr2[i].select=true
                }
        }
    }
 return arr2;
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.activityPage()
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})